package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.data.TitleVO;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.ExcelUtil;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.vo.DateListVO;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.AutomaticModelBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.AutomaticTableBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WuHanUnitBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.AutomaticModel;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.MeterReadingEnum;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/web/wuhan/automatic")
public class AutomaticTableAction extends BaseAction {

    @Autowired
    private AutomaticTableBiz automaticTableBiz;
    @Autowired
    private AutomaticModelBiz automaticModelBiz;
    @Autowired
    private WuHanUnitBiz wuHanUnitBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getAutomaticTableListPage")
    public ModelAndView getAutomaticTableListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/wuHanCommon/meterReading/meterReadingList");
        List<SelectVO> selectVOS = new ArrayList<>();
        MeterReadingEnum[] meterReadingEnums = MeterReadingEnum.values();
        for (MeterReadingEnum meterReadingEnum : meterReadingEnums) {
            selectVOS.add(new SelectVO(meterReadingEnum.getName(), "0," + meterReadingEnum.getCode()));
        }
        PointSystem[] pointSystems = PointSystem.values();
        for (PointSystem pointSystem : pointSystems) {
            selectVOS.add(new SelectVO(pointSystem.getName(), "1," + pointSystem.getCode()));
        }
        modelAndView.addObject("selectVOS", selectVOS);
        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getAutomaticTableListJSON")
    @ResponseBody
    public JSONObject getAutomaticTableListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                                @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                                @RequestParam(value = "orderBy", required = false) String orderBy,
                                                @RequestParam(value = "meterReading", required = false) String meterReading,
                                                @RequestParam(value = "select_unitId", required = false) String select_unitId,
                                                @RequestParam(value = "date", required = false) String date) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        if (StringUtils.isNotBlank(meterReading)) {
            String type = meterReading.split(",")[0];
            if ("0".equals(type)) {
                queryParam.getSqlMap().put("meterReading", meterReading.split(",")[1]);
            } else if ("1".equals(type)) {
                queryParam.getSqlMap().put("pointSystem", meterReading.split(",")[1]);
            }
        }
        queryParam.getSqlMap().put("unitId", select_unitId);
        queryParam.getSqlMap().put("autoTime", date);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return automaticTableBiz.findJSONList(queryParam);
    }


    /**
     * 获取系统模块选择数据
     *
     * @param q
     * @return
     */
    @RequestMapping("/getMeterReadingSelect")
    @ResponseBody
    public Set<SelectVO> getMeterReadingSelect(@RequestParam(value = "q", required = false) String q) {
        Set<SelectVO> selectVOS = new HashSet<>();
        MeterReadingEnum[] meterReadingEnums = MeterReadingEnum.values();
        for (MeterReadingEnum meterReadingEnum : meterReadingEnums) {
            selectVOS.add(new SelectVO(meterReadingEnum.getName(), meterReadingEnum.getCode()));
        }
        return selectVOS;
    }

    /**
     * 自动抄表生成
     */
    @RequestMapping("/generateAutomaticMeterReading")
    @ResponseBody
    public void generateAutomaticMeterReading(@RequestParam(value = "meterReading", required = false) String meterReading,
                                              @RequestParam(value = "select_unitId", required = false) String unitId,
                                              @RequestParam(value = "date", required = false) String date,
                                              HttpServletResponse response) {
        if (StringUtils.isBlank(meterReading) || StringUtils.isBlank(date) || !"0".equals(meterReading.split(",")[0])) {
            return;
        }
        MeterReadingEnum meterReadingEnum = MeterReadingEnum.valueOf(meterReading.split(",")[1]);
        List<TitleVO> titleVOS = MeterReadingEnum.getTitleVO(meterReadingEnum);
        if (titleVOS == null) {
            return;
        }
        String modelURLString = Configuration.getConfigurationByName(meterReadingEnum.getCode());
        WuHanUnit wuHanUnit = wuHanUnitBiz.findModel(StringConverters.ToLong(unitId));
        String excelName = meterReadingEnum.getName() + "点检表";
        try {
            ExcelUtil.ExcelByModel1(response, excelName, modelURLString, automaticTableBiz.getAutomaticTable_New(meterReadingEnum, date), titleVOS, wuHanUnit == null ? "" : wuHanUnit.getUnit(), date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化模板
     */
    @RequestMapping("/initAutomaticModel")
    @ResponseBody
    public ResultDto initAutomaticModel() {
        MeterReadingEnum[] meterReadingEnums = MeterReadingEnum.values();
        for (MeterReadingEnum m : meterReadingEnums) {
            List<TitleVO> titleVOS = MeterReadingEnum.getTitleVO(m);
            if (CollectionUtils.isNotEmpty(titleVOS)) {
                for (TitleVO titleVO : titleVOS) {
                    List<DateListVO> dateListVOS = automaticTableBiz.getDateList();
                    for (DateListVO dateListVO : dateListVOS) {
                        automaticModelBiz.addOrUpdate(new AutomaticModel(m
                                , dateListVO.getStartTime()
                                , dateListVO.getEndTime()
                                , dateListVO.getIndex(), titleVO.getTitle(), titleVO.getI()));
                    }
                }
            }
        }
        return ResultUtil.success();
    }
}
